Repository navigation
Support pickle and copy.deepcopy of dpnp.ndarray - #3142
antonwolfy wants to merge 3 commits into
Conversation
Both failed with "no default __reduce__ due to non-trivial __cinit__" because a SYCL queue can't be pickled. `__reduce__` copies the data to the host and pickles it with the array type and the USM type; unpickling allocates the array on the default device, keeping the USM type, the dtype (raising on a device that doesn't support it), the C/F layout and the subclass. `__deepcopy__` copies on the array's own queue, like `__copy__`, so a deep copy doesn't go through `__reduce__` to the default device. Enable the matching CuPy tests and update `test_pickle` in `test_npz.py` to its current upstream version.
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3142/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev3+81.g7a7e13082 ran successfully. |
|
@antonwolfy |
|
@ndgrigorian Comparing to CuPy, they seem also supporting only the default device. |
I think this sounds reasonable, if the dpctl PR ends up open soon (which it may be) we can rework this one slightly |
|
IntelPython/dpctl#2406 is now open |
# Conflicts: # CHANGELOG.md # dpnp/tests/test_sycl_queue.py
`__reduce__` now pickles the array's queue, which `dpctl` can do from 0.24.0dev1 on, so an unpickled array comes back on its own device instead of the default one. As with `dpctl`'s USM memory, pickling an array whose queue `dpctl` can't pickle (a sub-device, or a context that isn't the device's default one) raises `pickle.PicklingError`. Raise the `dpctl` dev pins to 0.24.0dev1.
pickle and copy.deepcopy of dpnp.ndarraypickle and copy.deepcopy of dpnp.ndarray
pickle.dumps(a)andcopy.deepcopy(a)failed on anydpnp.ndarraywithTypeError: no default __reduce__ due to non-trivial __cinit__. The default pickling tries to serialize the underlyingusm_ndarraytogether with its SYCL queue, and a SYCL queue couldn't be pickled.Closes #2602.
How pickling works (
__reduce__):asnumpy(order="A")and pickled together with the array type, the USM type and the SYCL queue.dpctlcan pickle aSyclQueuefrom0.24.0dev1on, so thedpctldev pins are raised to0.24.0dev1.dpctlrestores it. A queue with default properties comes back as the cached queue of its device. Arrays pickled in onepickle.dumpscall that share an"in_order"or"enable_profiling"queue come back on one new queue, so they can still be computed together.dpctl's USM memory objects, pickling an array whose queuedpctlcan't pickle (the device is a sub-device, or the context isn't the device's default context) raisespickle.PicklingError. Unpickling raisespickle.UnpicklingErrorif the device isn't available.ValueErroron a device that doesn't support it instead of silently downcastingfloat64tofloat32asdpnp.asarraywould.dpnp.ndarrayare preserved.copy.deepcopy(__deepcopy__) behaves likecopy.copy: it returnsa.copy(order="K")on the array's own queue, with no host round trip. It has to be defined explicitly; otherwisecopy.deepcopywould go through__reduce__and a host round trip.Docs:
ndarray.__reduce__is now listed indoc/reference/ndarray.rst.